define(["require", 'config/appregister','business/EnterpriseReg/service/reghttp', "jquery"], function(require, app, httpServices) {
	app.controller("enterpriseloginCtrl", function($scope, $http, $state,$cookies) {
		getcodepic();
		
		//点击忘记密码
		$scope.nopa = function() {
			$state.go("eng.loginedBack");
		};

		//读取cookie信息
		if($cookies.getObject('priseInfo')){
			$scope.userInfo=$cookies.getObject('priseInfo');
			$scope.username=$scope.userInfo.name;
			$scope.password=$scope.userInfo.pass;
		}
		//企业登录请求验证码图片
		function getcodepic() {
			var req = {
				type: 'GET',
				uri: 'companyLoginWithVerifyCode',
			};
			httpServices.getcodeHttpService(req).success(function(data){	
				$scope.$apply(function(){
					if(data.code==0){
						$scope.imgsrc = data.data.img;
						var arr = data.data.verifyToken;
						var arred = arr.split(",");
						$scope.verifyToken = arred[0];
						var expireTime = arred[1];
						console.log(expireTime);
						var exp = new Date();
						exp.setTime(exp.getTime()+expireTime);
						document.cookie = "verifyToken="+$scope.verifyToken+";expires="+exp.toGMTString();

					}else {
						alert(data.msg);
					}					
				})
			}).error(function(data){
				alert("验证码请求失败")
			})
			
			
		}

		var textemail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var textpswd = /^[\w]{6,16}$/;
		var textphone = /^1[3|4|5|7|8]\d{9}$/;

		$scope.bluRe = function() {
			var email = $("#input1").val();

			if(email == "") {
				$(".red").show();
				$(".red").html("请输入电子邮箱");
			} else {
				if(textemail.test(email)) {
					$(".red").hide();
					return false;
				} else {
					$(".red").show();
					$(".red").html("格式错误");
				}
				if(textphone.test(email)) {
					$(".red").hide();
					return false;
				} else {
					$(".red").show();
					$(".red").html("格式错误");
				}
			}
		}
		$scope.bluRp = function() {
			var pswd = $("#input2").val();
			if(pswd == "") {
				$(".red1").show();
				$(".red1").html("请输入密码");
			} else {
				if(textpswd.test(pswd)) {
					$(".red1").hide();
					return false;
				} else {
					$(".red1").show();
					$(".red1").html("密码格式错误");
				}
			}

		}

		//企业登录
		$scope.dengL = function() {
			var email = $("#input1").val();
			var pswd = $("#input2").val();
			var code = $("#input3").val().toUpperCase();
			
			var req = {
				type:'POST',
				uri:'companyLogin',
				data:{
					"email": email,
					"passwd": pswd,
					"verifyCode": code,
					"verifyToken": $scope.verifyToken
				}
			};
			httpServices.companylogin(req).success(function(res){
				if(res.code==0) {
					if($scope.remember){
						var data=new Date();
						data.setDate(data.getDate()+30);  //设置30天后过期
						$cookies.putObject('priseInfo',{name:$scope.username,pass:$scope.password},{
							domain:'www.gongchengjie.cn',
							expires:data      //时间格式
						});
					}
					alert("登录成功");
					$scope.$emit("loginEventSuccess", {
						authToken:(res.data.authToken.split(","))[0],
						seed:(res.data.seed.split(","))[0],
						type:"company"
					});
					//$state.go("comIndexCtrl");
				}else {
					alert(res.msg);
					getcodepic();
					return;					
				}
			}).error(function(res){
				alert("企业登录请求失败")
				getcodepic();
				return false;				
			});
		};
		
		//点击验证码图片 刷新
		$scope.toLoGin = function() {
			getcodepic();
		};
		
		

		//跳转个人登录/注册
		$scope.tomap = function() {
			$state.go("login");
		}
		$scope.tomaps = function() {
			$state.go("enterprisereg");
		}
	})
})